package com.hsshy.beam.excel.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.hsshy.beam.common.support.HttpKit;
import com.hsshy.beam.excel.domain.ExportInfo;
import com.hsshy.beam.excel.domain.Model;
import com.hsshy.beam.excel.domain.MultiLineHeadExcelModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:
 *
 * @author jianweiwang
 * @version v1.0
 * @link wjw@choicesoft.com.cn
 * @date 2019/3/29 18:21
 **/
@Api(value = "WriteExcelController")
@RestController
@RequestMapping(value = "/write")
public class WriteExcelController {

    public HttpServletResponse getExportExcelResponse(String title){
        HttpServletResponse response = HttpKit.getResponse();
        //web浏览通过MIME类型判断文件是excel类型
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // Content-disposition属性设置成以附件方式进行下载
        try {
            response.setHeader("Content-disposition", "attachment;filename=" +new String((title+ ".xlsx").getBytes(), "iso-8859-1") );
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return response;

    }

    @ApiOperation(value = "写ExportInfo")
    @RequestMapping(value = "/exportInfo", method = RequestMethod.GET)
    public void writeExportInfo() throws IOException {
        List<ExportInfo> list = getList();
        String sheetName = "第一个 sheet";
        OutputStream out = getExportExcelResponse("exportInfo").getOutputStream();
        ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLSX);
        Sheet sheet = new Sheet(1,0,ExportInfo.class);
        sheet.setSheetName(sheetName);
        writer.write(list,sheet);
        writer.finish();
    }

    @ApiOperation(value = "写Model")
    @RequestMapping(value = "/model", method = RequestMethod.GET)
    public void writeModel() throws IOException {
        List<Model> list = getModelList();
        String sheetName = "第一个 sheet";
        OutputStream out = getExportExcelResponse("Model").getOutputStream();

        ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLSX);
        Sheet sheet = new Sheet(1,0,ExportInfo.class);
        sheet.setSheetName(sheetName);
        writer.write(list,sheet);
        writer.finish();
    }


    @ApiOperation(value = "写MultiLineHeadExcelModel")
    @RequestMapping(value = "/multiLineHeadExcelModel", method = RequestMethod.GET)
    public void writeMultiLineHeadExcelModel() throws IOException {
        List<MultiLineHeadExcelModel> list = getMulitiList();
        String sheetName2 = "第二个 sheet";
        OutputStream out = getExportExcelResponse("MultiLineHeadExcelModel").getOutputStream();

        ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLSX);
        Sheet sheet = new Sheet(2,0,MultiLineHeadExcelModel.class);
        sheet.setSheetName(sheetName2);
        writer.write(list,sheet);
        writer.finish();
    }




    private List<MultiLineHeadExcelModel> getMulitiList() {
        List<MultiLineHeadExcelModel> list = new ArrayList<>();
        MultiLineHeadExcelModel model = new MultiLineHeadExcelModel();
        model.setP1("1");
        model.setP2("2");
        model.setP3(3);
        model.setP4(4);
        model.setP5("5");
        model.setP6("6");
        model.setP7("7");
        model.setP8("8");
        model.setP9("9");
        list.add(model);
        return list;
    }

    private List<ExportInfo> getList() {
        List<ExportInfo> list = new ArrayList<>();
        ExportInfo info = new ExportInfo();
        info.setAddress("aaaaaaaaaaa");
        info.setAge("22");
        info.setEmail("asdadad");
        info.setName("wjwei");
        list.add(info);
        return list;
    }

    private List<Model> getModelList() {
        List<Model> list = new ArrayList<>();
        Model model = new Model();
        model.setSex("男");
        model.setAge("22");
        model.setId("wjwei@@");
        model.setName("wjwei");
        list.add(model);
        return list;
    }
}
